package com.sensePlusVR.manager;

import com.alibaba.fastjson.JSON;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.FeedbackCountRespDto;
import com.sensePlusVR.bean.FeedbackListRespDto;
import com.sensePlusVR.bean.FeedbackReplyItem;
import com.sensePlusVR.bean.FeedbackReqDto;
import com.sensePlusVR.net.NetCallBack;
import com.sensePlusVR.net.RequestUtils;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.ConstantURL;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MySharedPreferences;

import org.apache.http.Header;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

@SuppressWarnings("deprecation")
public class FeedbackManager extends BaseManager{
	
	private static final String TAG = FeedbackManager.class.getSimpleName();
	private static FeedbackManager INSTANTS;
	public static final int EventFeedbackList = 0;
	public static final int EventFeedbackNoReadCount = 1;
	
	static public FeedbackManager getInstants(){
		if (INSTANTS == null){
			INSTANTS = new FeedbackManager();
		}
		return INSTANTS;
	}
	
	public Integer getFeedbackNoReadCount(){
		UploadTask task = new UploadTask();
		task.startFbCount();
		//TODO local
		return -1;
	}
	
	public List<FeedbackListRespDto> getFeedbackList(FeedbackReqDto feedbackReqDto){
		UploadTask task = new UploadTask(feedbackReqDto);
		task.startFbList();
		//TODO local
		return null;
	}
	
	public class UploadTask {
		
		private FeedbackReqDto feedbackReqDto;
		
		public UploadTask(){}
		
		public UploadTask(FeedbackReqDto feedbackReqDto){
			this.feedbackReqDto = feedbackReqDto;
			
		}
		
		private void updatefbListDB(List<FeedbackListRespDto> feedbackListRespDtos){
			
			FeedbackListRespDto.saveOrUpdate(feedbackListRespDtos);
			
			for(FeedbackListRespDto feedbackListRespDto : feedbackListRespDtos){
				
				FeedbackReplyItem.saveOrUpdate(feedbackListRespDto.getFeedbackReplyList());
				
			}
		}
		
		/**
		 * 获取Feedback列表
		 */
		public void startFbList() {
	    	String url = ConstantURL.FEEDBACK_LIST;
	    	String token =  MySharedPreferences.getString2(VRApplication.getAppContext(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_TOKEN);
	    	String json = JSON.toJSONString(feedbackReqDto);
	    	 RequestUtils.clientToken.addHeader(Constant.STR_X_AUTH_TOKEN,token);
	    	 RequestUtils.clientTokenPost(VRApplication.getAppContext(), url, new ByteArrayEntity(json.getBytes()), Constant.STR_APPLICATION_JSON, new NetCallBack(){
	    		 @Override
	    		public void onMySuccess(int statusCode, Header[] headers,
	    				JSONArray response) {
	    			super.onMySuccess(statusCode, headers, response);
	    			MyLog.d(TAG, "getFeedbackList onMySuccess statusCode : " + statusCode);
	    			if(response == null){
	    				MyLog.e(TAG, "getFeedbackList onMySuccess response == null");
	    				return;
	    			}
	    			MyLog.d(TAG, "getFeedbackList onMySuccess response : " + response);
	    			if(statusCode == Constant.HTTP_200){
	    				MyLog.d(TAG, "getFeedbackList successfully");
	    				List<FeedbackListRespDto> feedbackListRespDto = JSON.parseArray(response.toString(), FeedbackListRespDto.class);
	    				updatefbListDB(feedbackListRespDto);
	    				callEventListener(EventFeedbackList, feedbackListRespDto);
	    			}
	    		}
	    		 
	    		 @Override
	    		public void onMyFailure(int statusCode, Header[] headers,
	    				Throwable throwable, JSONObject errorResponse) {
	    			super.onMyFailure(statusCode, headers, throwable, errorResponse);
	      			MyLog.e(TAG, "getFeedbackList onMyFailure statusCode : " + statusCode);
	    	        if(statusCode == 0 || errorResponse == null){
	    	        	MyLog.e(TAG, "getFeedbackList errorJsonobject == null");
	    	        	return ;
	    	        }
	    	        MyLog.e(TAG, "getFeedbackList onMyFailure errorResponse : " + errorResponse.toString());
	    		}
	    		 
	    	 });
	    
			
		}
		
		/**
		 * 获取Feedback请求次数
		 */
		public void startFbCount() {

	    	String url = ConstantURL.FEEDBACK_NOT_READ_COUNT;
	    	String token =  MySharedPreferences.getString2(VRApplication.getAppContext(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_TOKEN);
	    	 RequestUtils.clientToken.addHeader(Constant.STR_X_AUTH_TOKEN,token);
	    	 RequestUtils.clientTokenPost(VRApplication.getAppContext(), url, new NetCallBack(){
	    		 
	    		 @Override
	    		public void onMySuccess(int statusCode, Header[] aheader,
	    				JSONObject jsonobject) {
	    			super.onMySuccess(statusCode, aheader, jsonobject);
	    			MyLog.d(TAG, "getFeedbackNoReadCount onMySuccess statusCode : " + statusCode);
	    			if(jsonobject == null){
	    				MyLog.e(TAG, "getFeedbackNoReadCount onMySuccess jsonobject == null");
	    				return;
	    			}
	    			MyLog.d(TAG, "getFeedbackNoReadCount onMySuccess jsonobject : " + jsonobject);
	    			if(statusCode == Constant.HTTP_200){
	    				FeedbackCountRespDto feedbackCountRespDto = JSON.parseObject(jsonobject.toString(),FeedbackCountRespDto.class);
	    				MyLog.d(TAG, "getFeedbackList successfully feedbackCountRespDto : " + feedbackCountRespDto.toString());
	    				callEventListener(EventFeedbackNoReadCount, feedbackCountRespDto);
	    			}
	    		}
	    		 
	    		 @Override
	    		public void onMyFailure(int statusCode, Header[] headers,
	    				Throwable throwable, JSONObject errorResponse) {
	    			super.onMyFailure(statusCode, headers, throwable, errorResponse);
	      			MyLog.e(TAG, "getFeedbackNoReadCount onMyFailure statusCode : " + statusCode);
	    	        if(statusCode == 0 || errorResponse == null){
	    	        	MyLog.e(TAG, "getFeedbackNoReadCount errorJsonobject == null");
	    	        	return ;
	    	        }
	    	        MyLog.e(TAG, "getFeedbackNoReadCount onMyFailure errorResponse : " + errorResponse.toString());
	    		}
	    		 
	    	 });
	    
			
		}
		
	}
	
}
